Odd Even Sort Algorithm

The Odd-Even Sort Algorithm, also known as Brick Sort, is a relatively simple sorting algorithm used to arrange a list of elements in a specific order, typically ascending or descending. This algorithm is based on the idea of comparing and swapping adjacent elements in the list, similar to the Bubble Sort algorithm. However, the unique feature of Odd-Even Sort is that it divides the list into two separate sections, consisting of odd-indexed and even-indexed elements. By performing multiple passes through the list, the algorithm swaps neighboring elements if they are in the wrong order, eventually sorting the entire list. In the first pass, the algorithm compares and swaps all pairs of elements with odd-even indices (i.e., (1,2), (3,4), (5,6), etc.). In the second pass, it does the same for all pairs of elements with even-odd indices (i.e., (2,3), (4,5), (6,7), etc.). These two passes are repeated until no more swaps are needed, indicating that the list is sorted. The simplicity of Odd-Even Sort makes it easy to understand and implement, but it also results in a relatively inefficient algorithm, with a worst-case time complexity of O(n^2). While it may not be suitable for large datasets or time-sensitive applications, the Odd-Even Sort Algorithm can still be useful for small-scale sorting tasks or educational purposes.
/* C++ implementation Odd Even Sort */
#include <iostream>
#include <vector>

using namespace std;

void oddEven(vector<int> &arr, int size)
{
	bool sorted = false;
	while (!sorted)
	{
		sorted = true;
		for (int i = 1; i < size - 1; i += 2) //Odd
		{
			if (arr[i] > arr[i + 1])
			{
				swap(arr[i], arr[i + 1]);
				sorted = false;
			}
		}

		for (int i = 0; i < size - 1; i += 2) //Even
		{
			if (arr[i] > arr[i + 1])
			{
				swap(arr[i], arr[i + 1]);
				sorted = false;
			}
		}
	}
}

void show(vector<int> A, int size)
{
	int i;
	for (i = 0; i < size; i++)
		cout << A[i] << "\n";
}

int main()
{
	int size, temp;
	cout << "\nEnter the number of elements : ";
	cin >> size;

	vector<int> arr;

	cout << "\nEnter the unsorted elements : \n";

	for (int i = 0; i < size; ++i)
	{
		cin >> temp;
		arr.push_back(temp);
	}

	oddEven(arr, size);

	cout << "Sorted array\n";
	show(arr, size);
	return 0;
}

LANGUAGE:

DARK MODE: